/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.fty.web;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.jeesite.common.web.http.ServletUtils;
import com.jeesite.modules.BeanMapUtil;
import com.jeesite.modules.ChinaFontProvide;
import com.jeesite.modules.Watermark;
import com.jeesite.modules.fty.entity.FtyLeaseContractFull;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.fty.entity.FtyContractTest;
import com.jeesite.modules.fty.service.FtyContractTestService;

import java.beans.IntrospectionException;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * fty_contract_testController
 * @author chensk
 * @version 2018-05-20
 */
@Controller
@RequestMapping(value = "${adminPath}/fty/ftyContractTest")
public class FtyContractTestController {

	@Autowired
	private FtyContractTestService ftyContractTestService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public FtyContractTest get(String id, boolean isNewRecord) {
		return ftyContractTestService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */

	@RequestMapping(value = {"list", ""})
	public String list(FtyContractTest ftyContractTest, Model model) {
		model.addAttribute("ftyContractTest", ftyContractTest);
		return "modules/fty/ftyContractTestList";
	}
	
	/**
	 * 查询列表数据
	 */

	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<FtyContractTest> listData(FtyContractTest ftyContractTest, HttpServletRequest request, HttpServletResponse response) {
		Page<FtyContractTest> page = ftyContractTestService.findPage(new Page<FtyContractTest>(request, response), ftyContractTest); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */

	@RequestMapping(value = "form")
	public String form(FtyContractTest ftyContractTest, Model model) {
		ftyContractTest.setValues(StringEscapeUtils.escapeXml(ftyContractTest.getValues()));
		model.addAttribute("ftyContractTest", ftyContractTest);
		return "modules/fty/ftyContractTestForm";
	}

	/**
	 * 保存fty_contract_test
	 */

	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated FtyContractTest ftyContractTest,String values) {

		ftyContractTest.setValues(StringEscapeUtils.unescapeXml(values));
		ftyContractTestService.saves(ftyContractTest);
		return ServletUtils.renderResult(Global.TRUE, "删除fty_contract_test成功！");
	}

	@GetMapping(value = "view")
	@ResponseBody
	public String view(@Validated FtyContractTest ftyContractTest,String values) {
		ftyContractTest = ftyContractTestService.get("998019908385574912");
		return  StringEscapeUtils.escapeXml(ftyContractTest.getValues());
	}
	
	/**
	 * 删除fty_contract_test
	 */

	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(FtyContractTest ftyContractTest) {
		ftyContractTestService.delete(ftyContractTest);
		return ServletUtils.renderResult(Global.TRUE, "删除fty_contract_test成功！");

	}

	/**
	 * 通过网站域名URL获取该网站的源码
	 *
	 * @param url
	 * @return String
	 * @throws Exception
	 */

	public static byte [] readInputStream(InputStream instream) throws Exception
	{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte [] buffer = new byte[1204];
		int len = 0;
		while ((len = instream.read(buffer)) != -1)
		{
			outStream.write(buffer, 0, len);
		}
		instream.close();
		return outStream.toByteArray();
	}


	public static String getURLSource(File url) throws Exception
	{
		InputStream inStream = new FileInputStream(url);
		// 通过输入流获取html二进制数据
		byte [] data = readInputStream(inStream); // 把二进制数据转化为byte字节数据
		String htmlSource = new String(data);

		inStream.close();
		return htmlSource;
	}

	@RequestMapping("/contract")
	public void sellercontract( FtyContractTest ftyContractTest, HttpServletRequest request,
							   HttpServletResponse response, HttpSession session)
			throws Exception {

		ftyContractTest = ftyContractTestService.get("998019908385574912");
		//Map<String, Object> map = BeanMapUtil.bean2Map(ftyContractTest);

		response.setContentType("application/pdf");
		response.setHeader("Expires", "0");// 不缓存
		response.setHeader("Cache-Control",
				"must-revalidate, post-check=0, pre-check=0");
		response.setHeader("Pragma", "public");

		InputStream cssStream = request.getServletContext().getResourceAsStream("/static/fang/style/word.css");

		// 如果不想立即下载，屏蔽下面一行，就是浏览器中浏览了
		// response.setHeader("Content-disposition",
		// "attachment; filename = contract.pdf");
		String contract =ftyContractTest.getValues();
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
				"UniGB-UCS2-H", false);
		Font fontBold = new Font(bfChinese, 25, Font.NORMAL);

		//要替换的字符名称
		String lender = "";
		try {
			Document document = new Document();
			ServletOutputStream out = response.getOutputStream();
			PdfWriter writer = PdfWriter.getInstance(document, out);// 输出到浏览器

			//	InputStream cssis = new ByteArrayInputStream(cssSource.toString().getBytes());
//			print.setP
			//writer.setPageEvent(new Watermark(ftyLeaseContractFull.getCNumber()));//水印内容 添加事件 在事件中添加水印
			// 若果是要输出到文件，取消下面的注释，屏蔽response的设置
			// PdfWriter writer = PdfWriter.getInstance(document,new
			// FileOutputStream("protocol.pdf"));

			document.open();
			//文字水印
			//添加水印和页码
//			Paragraph title = new Paragraph("房屋租凭中介合同", fontBold);
//			title.setAlignment(Element.ALIGN_CENTER);
//			document.add(title);

			ByteArrayInputStream bin = new ByteArrayInputStream(contract.getBytes());
			XMLWorkerHelper.getInstance().parseXHtml(writer,document, bin,  cssStream, new ChinaFontProvide());

			document.close();
			out.close();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}